package com.yunxi.service.dubboService.impl.iam2pms;

import com.yunxi.baseDataToHub.pojo.iam.IamOrg;
import com.yunxi.baseDataToHub.pojo.iam.IamPos;
import com.yunxi.baseDataToHub.pojo.iam.IamStaff;
import com.yunxi.core.util.WgrDate;
import com.yunxi.hubToBaseData.service.Iam2BaseDataDubboService;
import com.yunxi.model.sys.Org;
import com.yunxi.model.sys.Pos;
import com.yunxi.model.sys.Staff;
import com.yunxi.service.localService.sys.OrgService;
import com.yunxi.service.localService.sys.PosService;
import com.yunxi.service.localService.sys.StaffService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;

public class Iam2BaseDataDubboServiceImpl implements Iam2BaseDataDubboService {
    private static final Logger logger = LoggerFactory.getLogger(Iam2BaseDataDubboServiceImpl.class);

    @Autowired
    @Qualifier("orgServiceImpl")
    OrgService orgService;

    @Autowired
    @Qualifier("staffServiceImpl")
    StaffService staffService;

    @Autowired
    @Qualifier("posServiceImpl")
    PosService posService;

    @Override
    public void syncOrg(List<IamOrg> iamOrgList) throws Exception {
        if(iamOrgList!=null){
            for (int i=0;i<iamOrgList.size();i++)
            {
                IamOrg  o = iamOrgList.get(i);
                Org org = new Org();
                org.setOrgId(o.getOId());
                org.setPid(o.getPOIdOrgAdmin());
                org.setOrgName(o.getName());
                org.setOrgType(1);//0店端1厂端
                org.setCreateTime(WgrDate.getCurrentDate());
                orgService.saveOrUpdate(org);
            }
        }
    }

    @Override
    public void syncStaff(List<IamStaff> iamStaffList) throws Exception {
        if(iamStaffList!=null){
            for (int i=0;i<iamStaffList.size();i++)
            {
                IamStaff  o = iamStaffList.get(i);
                Staff staff = new Staff();
                staff.setStaffId(o.getOId());
                staff.setOrgId(o.getOIdDepartment());
                staff.setStaffName(o.getName());
                staff.setPositionId(o.getOIdJobPost());
                staff.setAccountNo(o.getAccountNo());
                staff.setGrade(o.getOidJobGrade());
                staff.setLv(o.getOIdJobLevel());
                staff.setEmployType(o.getEmployType());
                staff.setMobilePhone(o.getMobilePhone());
                staff.setCreateTime(WgrDate.getCurrentDate());
                staff.setPictureUrl(o.getPictureUrl());
                staff.setGender(o.getGender());

                staff.setStatus(1);
                if(o.getIsDeleted().equals("true")){
                    staff.setStatus(0);
                    staff.setIsDel(1);
                }else{
                    staff.setIsDel(0);
                }
                if(StringUtils.isNotEmpty(o.getEntryStatus()))
                staff.setStaffStatus(Integer.parseInt(o.getEntryStatus()));
                staffService.syncStaff(staff);
            }
        }
    }

    @Override
    public void syncPos(List<IamPos> iamPosList) throws Exception {
        if(iamPosList!=null){
            for (int i=0;i<iamPosList.size();i++)
            {
                IamPos  o = iamPosList.get(i);
                Pos pos = new Pos();
                pos.setPositionId(o.getOId());
                pos.setPositionCode(o.getCode());
                pos.setPositionName(o.getName());
                pos.setSortNo(o.getOrder());
                pos.setStatus(o.getStatus());
                pos.setLv(o.getLv());
                pos.setCreateTime(WgrDate.getCurrentDate());
                pos.setType(1);//0店端1厂端
                posService.saveOrUpdate(pos);
            }
        }
    }
}
